package com.cssw.fyzb.core.dao.pay;

import com.cssw.fyzb.core.model.dodb.QuickPayOrder;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author: yangyiqun
 * @Date: 2021/2/17 14:20
 */
@Repository
public class QuickPayOrderDaoImpl implements QuickPayOrderDao {

    @Override
    public Long save(Long userId, String certCode, String busiType, BigDecimal amount, String goodsName, String goodsDesc, String orderNo, String busiNo) {
        QuickPayOrder order = new QuickPayOrder();
        order.setUserId(userId);
        order.setCertCode(certCode);
        order.setBusiType(busiType);
        order.setAmount(amount);
        order.setGoodsName(goodsName);
        order.setGoodsDesc(goodsDesc);
        order.setOrderNo(orderNo);
        order.setBusiNo(busiNo);
        order.setCreateTime(new Date());
        order.setStatus(QuickPayOrder.STATUS_INIT);
        order.save();
        return order.getPayId();
    }

    @Override
    public void updateSuccess(Long payId) {
        QuickPayOrder order = new QuickPayOrder();
        order.setPayId(payId);
        order.setStatus(QuickPayOrder.STATUS_SUCC);
        order.setUpdateTime(new Date());
        order.update();
    }

    @Override
    public void updateFail(Long payId, String errorCode, String errorMsg) {
        QuickPayOrder order = new QuickPayOrder();
        order.setPayId(payId);
        order.setStatus(QuickPayOrder.STATUS_FAIL);
        order.setUpdateTime(new Date());
        order.setErrorCode(errorCode);
        order.setErrorMsg(errorMsg);
        order.update();
    }

    @Override
    public QuickPayOrder getByOrderNo(String orderNo) {
        String sql = "select * from t_quick_pay_order where order_no = ?";
        return QuickPayOrder.dao.findFirst(sql, orderNo);
    }
}
